package com.tzinfo.tzt.util;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

/**
 * Created by tongwanglin on 14-1-2.
 */
public class CacheManager {
	public static String OLD_MAP_CACHE="old_map_cache";
	public static String NEW_MAP_CACHE="new_map_cache";
	public static String RENT_MAP_CACHE="rent_map_cache";


	public static String getCache(Context mContext,String cacheName) {
		String json = null;
		FileInputStream inputStream=null;
		try {
			inputStream = mContext.openFileInput(cacheName
					+ ".json");
			if (inputStream != null) {
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int length = -1;
				while ((length = inputStream.read(buffer)) != -1) {
					stream.write(buffer, 0, length);
				}
				json = stream.toString();
				stream.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}finally{
			try {
				if(inputStream!=null){
					inputStream.close();
					inputStream=null;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return json;
	}


	public static void updateCache(Context mContext,String cacheName, String jsonContent) {
		try {
			FileOutputStream outputStream = mContext.openFileOutput(cacheName
					+ ".json", Context.MODE_WORLD_READABLE);
			outputStream.write(jsonContent.getBytes());
			outputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
